﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PositionCalculator.cs" company="">
//   
// </copyright>
//  <summary>
//   The position calculator.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Shatranj
{
    using System;
    using System.Text.RegularExpressions;

    /// <summary>
    /// The position calculator.
    /// </summary>
    public class PositionCalculator
    {
        // in C# 4.0 can be made dynamic
        #region Public Methods and Operators

        /// <summary>
        /// The parse.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <returns>
        /// The <see cref="BoardLocation"/>.
        /// </returns>
        public static BoardLocation Parse(string position)
        {
            var regex = new Regex(@"(?<file>^[a-h])(?<pos>\d{1,2})", RegexOptions.IgnoreCase);
            var match = regex.Match(position);
            return new BoardLocation
                       {
                           Y = char.Parse(match.Groups["file"].Value) - 'a', 
                           X = int.Parse(match.Groups["pos"].Value) - 1
                       };
        }

        /// <summary>
        /// The parse for board position.
        /// </summary>
        /// <param name="position">
        /// The position.
        /// </param>
        /// <returns>
        /// The <see cref="BoardLocation"/>.
        /// </returns>
        public static BoardLocation ParseForBoardPosition(string position)
        {
            var point = Parse(position);
            point.X = Math.Abs(point.X - 7);
            point.Y = point.Y;
            return point;
        }

        #endregion
    }
}